﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace MainForm
{
    [Serializable]
    public class GameEvent
    {
        public GameEvent()
        {
            this.ID = Guid.NewGuid().ToString();
            this.Handled = false;
        }
        public string ID { get; set; }
        public long TimeMilliSecond { get; set; }
        public string EventName { get; set; }
        public SizeF TextSize { get; internal set; }
        public bool Handled { get; set; }
        public override string ToString()
        {
            var ts = new TimeSpan(this.TimeMilliSecond * 10000);
            var ms = ts.Seconds + ts.Milliseconds / 1000d;
            return string.Format("{0}分{1:N2}秒 --- {2}", ts.Minutes, ms, this.EventName);
        }

    }
    [Serializable]
    public class GameEvents
    {
        public GameEvents()
        {
            this.Events = new List<GameEvent>();
        }
        public List<GameEvent> Events { get; set; }
        public string Name { get; set; }
        public void Save(string filePath)
        {
            var formatter = new BinaryFormatter();
            using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                formatter.Serialize(stream, this);
            }
        }
        public static GameEvents Load(string filePath)
        {
            try
            {
                var formatter = new BinaryFormatter();
                using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    return (GameEvents)formatter.Deserialize(stream);
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
